package com.cloudkinto.service.rabbit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.OperateRecordDo;
import com.cloudkinto.service.operate.OperateRecordService;
import com.cloudkinto.vo.operate.OperateReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class OperateRecordConsumer {
    @Autowired
    private OperateRecordService operateRecordService;

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.CrossMall_CMOrder_Operate))
    public void insertOperateRecord(String msg) {
        OperateReq object = JSONObject.parseObject(msg, OperateReq.class);
        try {
            operateRecordService.operateAddRecordAndValue(
                    object.getContent(),
                    object.getContentValue(),
                    object.getThirdId(),
                    object.getUserId(),
                    object.getUsername(),
                    object.getType(),
                    new Date()
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.CrossMall_CMOrder_OperateList))
    public void insertOperateRecordList(String msg) {
        List<OperateRecordDo> list = JSONArray.parseArray(msg, OperateRecordDo.class);
        try {
            operateRecordService.insertBatchMy(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
